//
//  DSHomeViewModel.m
//  DSJY
//
//  Created by 冯华强 on 16/10/18.
//  Copyright © 2016年 dsjy. All rights reserved.
//  首页VM

#import "DSHomeViewModel.h"
#import "DSAccountViewModel.h"
#import "DSCourse.h"
#import "DSAppResAddressIp.h"

@interface DSHomeViewModel ()
//模型
@property (nonatomic, strong, readwrite) NSMutableArray<DSCourse *> *courseList;
@end

@implementation DSHomeViewModel

#pragma mark - 懒加载
- (NSMutableArray<DSCourse *> *)courseList
{
    if (_courseList == nil) {
        _courseList = [NSMutableArray<DSCourse *> array];
    }
    return _courseList;
}

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        //修改条数
        self.pageCount = 100;
        //绑定事件
        [self initialBind];
    }
    return self;
}

- (void)initialBind
{
    @weakify(self);
    //初始化数据
    [self createCmdSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [DSNetworkTool indexWithParams:[DSAccountViewModel sharedAccount].account pagenum:self.pageNum pagecount:self.pageCount success:^(NSDictionary *responseObject) {
            
            //返回值
            self.response = [YQResponse mj_objectWithKeyValues:responseObject];
            [self veriResponse:self.response finishedCallBack:^{
                //列表
                NSArray<DSCourse *> *courseList = [DSCourse mj_objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                
                NSArray<NSString *> *appResAddressIps = [responseObject[@"attrs"] objectForKey:@"appResAddressIps"];
                [DSAppResAddressIp sharedInstance].appIds = appResAddressIps;
                
                if (self.pageNum == 1) {
                    [self.courseList removeAllObjects];
                }
                [self.courseList addObjectsFromArray:courseList];
                
                [subscriber sendNext:nil];
            }];
            [subscriber sendCompleted];
        } failure:^(NSError *error) {
            MTLog(@"%@", error);
//            [SVProgressHUD showErrorWithStatus:@"系统异常!"];
            [subscriber sendNext:[YQResponse errorResponseWithMsg:@"系统异常!"]];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    //设置请求时的提示框
    [self executingWithCommand:self.requestCommand];
}

@end
